/*
 * @Author: linweizeng 2630208719@qq.com
 * @Date: 2025-02-10 15:40:31
 * @LastEditors: linweizeng 2630208719@qq.com
 * @LastEditTime: 2025-02-18 14:17:06
 * @FilePath: \forum\src\utils\electron.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
let elec = (window as any).electron
export function openWindow(path: any, option: any = "", specs: any, windowFlag: Boolean = true) {
    let str = '';
    let arr = [];
    if (option) {
        for (const key in option) {
            if (Object.hasOwnProperty.call(option, key)) {
                const element = option[key];
                arr.push(`${key}=${encodeURIComponent(element)}`)
            }
        }
        str = `?${arr.join('&')}`
    }
    let electron = (window as any).electron

    if (electron) {
        let area = {
            width: 1000,
            height: 1000
        }
        if (specs) {
            let specsList = specs.split(',')
            specsList.forEach(item => {
                if (item.includes('width')) {
                    let width = item.split("=")
                    area.width = width.length > 1 ? parseInt(width[1]) : 1000;
                }
                if (item.includes('height')) {
                    let height = item.split("=")
                    area.height = height.length > 1 ? parseInt(height[1]) : 1000;
                }
            })
        }
        electron.openNewWindow && electron.openNewWindow(`${path}${str}`, specs ? area : null, windowFlag);
    } else {
        let originUrl = location.origin;
        if (windowFlag) {
            window.open(`${originUrl}/#${path}${str}`, "", `popup=true,${specs}`);
        } else {
            window.open(`${path}${str}`, "", `popup=true,${specs}`);
        }

        // window.location.href = `${originUrl}/#${path}${str}`
    }
}

export function openPath(url:any = ''){
    if(elec){
        elec.openPath(url)
    }
}

export function errorPath(locality:any = '', localUrl:any = '', url = ''){
    if(elec){
        return elec.errorPath(locality, localUrl, url)
    }
}

export function existsSync(url:any = ''){
    if(elec){
        return elec.existsSync(url)
    }
}

export function getAppPort(){
    if(elec){
        return elec.getAppPort()
    }
}

export function getAppSave(){
    if(elec){
        return elec.getAppSave()
    }
}

export function getAllLink(link:any = ''){
    if(elec){
        return elec.getAllLink(link)
    }
}


export function getLink(link = ''){
    if(elec){
        return elec.getLink(link)
    }
}


export function deleteCommon(link = ''){
    if(elec){
        return elec.deleteCommon(link)
    }
}